import XLSX from 'xlsx'

export default {

    exportExcel({fileName,header,headerDisplay,jsonArr,filePath}){

        return new Promise((resolve, reject) => {
            try{
                if(!filePath){
                    reject("excel导出路径不存在")
                }
                const newData = [headerDisplay, ...jsonArr];
                /* json数组转换excel */
                let worksheet = XLSX.utils.json_to_sheet(newData,{header:header,skipHeader:true});
                let new_workbook = XLSX.utils.book_new();
                XLSX.utils.book_append_sheet(new_workbook, worksheet, "sheet1");
                /* 生成文件，导出D盘 */
                let path  = filePath ? filePath : "D://" + fileName
                XLSX.writeFile(new_workbook, path);
                resolve("excel导出成功,路径" + path);
            } catch (err){
                reject("excel导出错误");
            }

        })

    }

}
